package com.pcjavanet.generate;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class CopyResource {
	private String outputAbsolutePath;
	private String outputBasePackagePath;
	
	private String requestUtilPackagePath;
	private String requestUtilOutputAbsoluetePath;

	private String qryConditionPackagePath;
	private String qryConditionOutputAbsoluetePath;

	private String qryTermPackagePath;
	private String qryTermOutputAbsoluetePath;

	private String serviceUtilPackagePath;
	private String serviceUtilOutputAbsoluetePath;

	private String requestUtilFile = "RequestUtil.java";
	private String qryConditionFile = "QryCondition.java";
	private String qryTermFile = "QryTerm.java";
	private String serviceUtil = "ServiceUtil.java";
	public String getOutputAbsolutePath() {
		return outputAbsolutePath;
	}

	public void setOutputAbsolutePath(String outputAbsolutePath) {
		this.outputAbsolutePath = outputAbsolutePath;
	}

	public String getOutputBasePackagePath() {
		return outputBasePackagePath;
	}

	public void setOutputBasePackagePath(String outputBasePackagePath) {
		this.outputBasePackagePath = outputBasePackagePath;
	}

	public String getRequestUtilPackagePath() {
		return requestUtilPackagePath;
	}

	public void setRequestUtilPackagePath(String requestUtilPackagePath) {
		this.requestUtilPackagePath = requestUtilPackagePath;
	}

	public String getRequestUtilOutputAbsoluetePath() {
		return requestUtilOutputAbsoluetePath;
	}

	public void setRequestUtilOutputAbsoluetePath(
			String requestUtilOutputAbsoluetePath) {
		this.requestUtilOutputAbsoluetePath = requestUtilOutputAbsoluetePath;
	}

	public String getQryConditionPackagePath() {
		return qryConditionPackagePath;
	}

	public void setQryConditionPackagePath(String qryConditionPackagePath) {
		this.qryConditionPackagePath = qryConditionPackagePath;
	}

	public String getQryConditionOutputAbsoluetePath() {
		return qryConditionOutputAbsoluetePath;
	}

	public void setQryConditionOutputAbsoluetePath(
			String qryConditionOutputAbsoluetePath) {
		this.qryConditionOutputAbsoluetePath = qryConditionOutputAbsoluetePath;
	}

	public String getQryTermPackagePath() {
		return qryTermPackagePath;
	}

	public void setQryTermPackagePath(String qryTermPackagePath) {
		this.qryTermPackagePath = qryTermPackagePath;
	}

	public String getQryTermOutputAbsoluetePath() {
		return qryTermOutputAbsoluetePath;
	}

	public void setQryTermOutputAbsoluetePath(String qryTermOutputAbsoluetePath) {
		this.qryTermOutputAbsoluetePath = qryTermOutputAbsoluetePath;
	}

	public String getServiceUtilPackagePath() {
		return serviceUtilPackagePath;
	}

	public void setServiceUtilPackagePath(String serviceUtilPackagePath) {
		this.serviceUtilPackagePath = serviceUtilPackagePath;
	}

	public String getServiceUtilOutputAbsoluetePath() {
		return serviceUtilOutputAbsoluetePath;
	}

	public void setServiceUtilOutputAbsoluetePath(
			String serviceUtilOutputAbsoluetePath) {
		this.serviceUtilOutputAbsoluetePath = serviceUtilOutputAbsoluetePath;
	}

	
	private void setAllPath(){
		this.qryConditionPackagePath = this.outputBasePackagePath + ".model";
		this.qryConditionOutputAbsoluetePath = this.outputAbsolutePath +this.qryConditionPackagePath.replace(".", File.separator) + File.separator + qryConditionFile;
		
		this.qryTermPackagePath = this.outputBasePackagePath + ".model";
		this.qryTermOutputAbsoluetePath = this.outputAbsolutePath +this.qryTermPackagePath.replace(".", File.separator) + File.separator + qryTermFile;
		
		this.requestUtilPackagePath = this.outputBasePackagePath + ".web.action";
		this.requestUtilOutputAbsoluetePath = this.outputAbsolutePath +this.requestUtilPackagePath.replace(".", File.separator) + File.separator + requestUtilFile;
		
		this.serviceUtilPackagePath = this.outputBasePackagePath + ".service";
		this.serviceUtilOutputAbsoluetePath = this.outputAbsolutePath +this.serviceUtilPackagePath.replace(".", File.separator) + File.separator + serviceUtil;
	}
	
	private String getContent(File file,String packageInfo){
		StringBuffer buf = new StringBuffer();
		buf.append("package ").append( packageInfo ).append(";\r\n");
		try {
			FileReader read = new FileReader( file );
			BufferedReader br = new BufferedReader(read);
			String row ; 
			while ((row = br.readLine()) != null) {
				buf.append(row).append("\r\n");
			}
		} catch (Exception e){
			e.printStackTrace();
		}
		return buf.toString() ; 
	}
	
	private void copy(){
		String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		File lf = new File(path);
		if (!lf.exists())
			System.out.println("null EXIST");
		File[] files = lf.listFiles();
		if (files.length == 0)
			System.out.println("null File");
//		private String requestUtilFile = "RequestUtil.java";
//		private String qryConditionFile = "QryCondition.java";
//		private String qryTermFile = "QryTerm.java";
//		private String serviceUtil = "ServiceUtil.java";
		for (int i = 0; i < files.length; i++) {
			if ( files[ i ].getName().equalsIgnoreCase(  requestUtilFile ) ) {
				String content = getContent( files[i] ,this.getRequestUtilPackagePath() );
				FileUtil.contentToSave(this.getRequestUtilOutputAbsoluetePath(), content) ;
			}
			if ( files[ i ].getName().equalsIgnoreCase(  qryConditionFile ) ) {
				String content = getContent( files[i] ,this.getQryConditionPackagePath() );
				FileUtil.contentToSave(this.getQryConditionOutputAbsoluetePath(), content) ;
			}
			if ( files[ i ].getName().equalsIgnoreCase(  qryTermFile ) ) {
				String content = getContent( files[i] ,this.getQryTermPackagePath() );
				FileUtil.contentToSave(this.getQryTermOutputAbsoluetePath(), content) ;
			}
			if ( files[ i ].getName().equalsIgnoreCase(  serviceUtil ) ) {
				String content = getContent( files[i] ,this.getServiceUtilPackagePath() );
				FileUtil.contentToSave(this.getServiceUtilOutputAbsoluetePath(), content) ;
			}
	 
		}
	}
	//
	public static void main(String args[]) {
		String path = Thread.currentThread().getContextClassLoader()
				.getResource("").getPath();
		System.out.println(path);
		File af = new File(path);
		if (!af.exists())
			System.out.println("null EXIST");
		;
		String[] files = af.list();
		if (files.length == 0)
			System.out.println("nullLENGTH");
		;
		for (int i = 0; i < files.length; i++) {
			if (files[i] != null)
				System.out.println(files[i]);
			else
				System.out.println("null");
		}

	}

	public void run() {
		setAllPath();
		copy();
	}

}
